'use strict';

module.exports = app => {
  class UserService extends app.Service {
        /*
            方法名:用户名是否可用
        */

    async recortIsExist(key, value) {
      const reslut = await app.db.query(`select count(uid) from db_users where ${key} = ?`, [ value ]);
      return !reslut.count;
    }

    async unameIsUsed(uname) {
      return this.recortIsExist('uname', uname);
    }

    async uemailIsUsed(uemail) {
      return this.recortIsExist('uemail', uemail);
    }

    async utellIsUsed(utel) {
      return this.recortIsExist('utel', utel);
    }

    async isUsed(uname, uemail, utel) {
      const reslut = await Promise.all([
        this.uemailIsUsed(uemail),
        this.unameIsUsed(uname),
        this.utellIsUsed(utel),
      ]);
      const ret = true;
      reslut.map(it => it && ret);
      return it;
    }
    }
  return UserService;
};
